home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 742 / rkrm_lib2 / rkrm_lib2.lha / Exec_Library / Interrupts / timersoftint.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  7KB  |  163 lines

  1. ;/* timersoftint.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -d0 -cfistq -v -y -j73 timersoftint.c
  3. Blink FROM LIB:c.o,timersoftint.o TO timersoftint LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6. timersoftint.c - Timer device software interrupt message port example.
  7.  
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. #include <exec/memory.h>
  34. #include <exec/interrupts.h>
  35. #include <devices/timer.h>
  36. #include <dos/dos.h>
  37. #include <clib/exec_protos.h>
  38. #include <clib/dos_protos.h>
  39. #include <clib/alib_protos.h>
  40. #include <stdio.h>
  41.  
  42. #ifdef LATTICE
  43. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  44. void chkabort(void) { return; }  /* really */
  45. #endif
  46.  
  47. #define MICRO_DELAY 1000
  48. #define OFF     0
  49. #define ON      1
  50. #define STOPPED 2
  51.  
  52. struct TSIData {
  53.     ULONG tsi_Counter;
  54.     ULONG tsi_Flag;
  55.     struct MsgPort *tsi_Port;
  56. };
  57.  
  58. struct TSIData *tsidata;
  59.  
  60. void tsoftcode(void);    /* Prototype for our software interrupt code */
  61.  
  62. void main(void)
  63. {
  64.     struct MsgPort *port;
  65.     struct Interrupt *softint;
  66.     struct timerequest *tr;
  67.  
  68.     ULONG endcount;
  69.  
  70.     /* Allocate message port, data & interrupt structures. Don't use CreatePort() */
  71.     /* or CreateMsgPort() since they allocate a signal (don't need that) for a    */
  72.     /* PA_SIGNAL type port. We need PA_SOFTINT.                                   */
  73.     if (tsidata = AllocMem(sizeof(struct TSIData), MEMF_PUBLIC|MEMF_CLEAR))
  74.     {
  75.         if(port = AllocMem(sizeof(struct MsgPort), MEMF_PUBLIC|MEMF_CLEAR))
  76.         {
  77.             NewList(&(port->mp_MsgList));                             /* Initialize message list */
  78.             if (softint = AllocMem(sizeof(struct Interrupt), MEMF_PUBLIC|MEMF_CLEAR))
  79.             {
  80.                 /* Set up the (software)interrupt structure. Note that this task runs at  */
  81.                 /* priority 0. Software interrupts may only be priority -32, -16, 0, +16, */
  82.                 /* +32. Also not that the correct node type for a software interrupt is   */
  83.                 /* NT_INTERRUPT. (NT_SOFTINT is an internal Exec flag). This is the same  */
  84.                 /* setup as that for a software interrupt which you Cause(). If our       */
  85.                 /* interrupt code was in assembler, you could initialize is_Data here to  */
  86.                 /* contain a pointer to shared data structures. An assembler software     */
  87.                 /* interrupt routine would receive the is_Data in A1.                     */
  88.  
  89.                 softint->is_Code = tsoftcode;    /* The software interrupt routine */
  90.                 softint->is_Data = tsidata;
  91.                 softint->is_Node.ln_Pri = 0;
  92.  
  93.                 port->mp_Node.ln_Type = NT_MSGPORT;       /* Set up the PA_SOFTINT message port  */
  94.                 port->mp_Flags = PA_SOFTINT;              /* (no need to make this port public). */
  95.                 port->mp_SigTask = (struct Task *)softint;     /* pointer to interrupt structure */
  96.  
  97.                 /* Allocate timerequest */
  98.                 if (tr = (struct timerequest *) CreateExtIO(port, sizeof(struct timerequest)))
  99.                 {
  100.                     /* Open timer.device. NULL is success. */
  101.                     if (!(OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest *)tr, 0)))
  102.                     {
  103.                         tsidata->tsi_Flag = ON;        /* Init data structure to share globally. */
  104.                         tsidata->tsi_Port = port;
  105.  
  106.                         /* Send of the first timerequest to start. IMPORTANT: Do NOT   */
  107.                         /* BeginIO() to any device other than audio or timer from      */
  108.                         /* within a software or hardware interrupt. The BeginIO() code */
  109.                         /* may allocate memory, wait or perform other functions which  */
  110.                         /* are illegal or dangerous during interrupts.                 */
  111.                         printf("starting softint. CTRL-C to break...\n");
  112.  
  113.  
  114.                         tr->tr_node.io_Command = TR_ADDREQUEST;    /* Initial iorequest to start */
  115.                         tr->tr_time.tv_micro = MICRO_DELAY;        /* software interrupt.        */
  116.                         BeginIO((struct IORequest *)tr);
  117.  
  118.                         Wait(SIGBREAKF_CTRL_C);
  119.                         endcount = tsidata->tsi_Counter;
  120.                         printf("timer softint counted %ld milliseconds.\n", endcount);
  121.  
  122.                         printf("Stopping timer...\n");
  123.                         tsidata->tsi_Flag = OFF;
  124.  
  125.                         while (tsidata->tsi_Flag != STOPPED) Delay(10);
  126.  
  127.                         CloseDevice((struct IORequest *)tr);
  128.                     }
  129.                     else printf("couldn't open timer.device\n");
  130.                     DeleteExtIO(tr);
  131.                 }
  132.                 else printf("couldn't create timerequest\n");
  133.                 FreeMem(softint, sizeof(struct Interrupt));
  134.             }
  135.             FreeMem(port, sizeof(struct MsgPort));
  136.         }
  137.         FreeMem(tsidata, sizeof(struct TSIData));
  138.     }
  139. }
  140.  
  141.  
  142. void tsoftcode(void)
  143. {
  144.     struct timerequest *tr;
  145.  
  146.     /* Remove the message from the port. */
  147.     tr = (struct timerequest *)GetMsg(tsidata->tsi_Port);
  148.  
  149.     /* Keep on going if main() hasn't set flag to OFF. */
  150.     if ((tr) && (tsidata->tsi_Flag == ON))
  151.     {
  152.         /* increment counter and re-send timerequest--IMPORTANT: This         */
  153.         /* self-perpetuating technique of calling BeginIO() during a software */
  154.         /* interrupt may only be used with the audio and timer device.        */
  155.         tsidata->tsi_Counter++;
  156.         tr->tr_node.io_Command = TR_ADDREQUEST;
  157.         tr->tr_time.tv_micro = MICRO_DELAY;
  158.         BeginIO((struct IORequest *)tr);
  159.     }
  160.     /* Tell main() we're out of here. */
  161.     else tsidata->tsi_Flag = STOPPED;
  162. }
  163.